﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using Foothill.WF.Command;
using Foothill.Utils;
using Foothill.WF.Core;
using GGFW.Utils.Constants;
using GGFW.Entity.DocumentsManage;
using GGFW.BusinessLogic.DocumentsManage;

namespace GGFW.Command.DocumentsManage
{
    public class ClientCommand : DispatchCommand
    {
        /// <summary>
        /// 客户信息业务逻辑访问类
        /// </summary>
        private readonly ClientControl ClientCtrl = new ClientControl();

        #region 分页获取客户信息
        /// <summary>
        /// </summary>
        /// <param name="context">HttpContext</param>
        /// <param name="requestType">RequestType</param>
        /// <param name="url">url</param>
        /// <returns>返回客户信息</returns>
        public ResultBase onSearchValidClientInfo(HttpContext context, RequestType requestType, string url)
        {
            //返回列表集合
            ResultBase result = null;

            try
            {
                string start = context.Request["start"];
                string limit = context.Request["limit"];
                int intStart = NumericUtil.toInteger(start, 0);
                int intLimit = NumericUtil.toInteger(limit, 1);
                if (intLimit == 0) intLimit = 1;
                int pageNo = intStart / intLimit + 1;
                int pageSize = intLimit;

                string queryName = context.Request["query"];

                result = ClientCtrl.searchClientInfo(pageSize, pageNo, queryName);
                result.Success = true;
            }
            catch (Exception ex)
            {
                result = new ResultBase();
                result.Success = false;
                result.Message = ex.Message;
                result.AddErrorDetail((int)GGFW.Utils.Constants.ErrorCode.ERROR_EXCPETION, ex.Message);
                log.Fatal(ex.Message);
            }
            return result;
        }
        #endregion

        #region 获取客户列表
        /// <summary>
        ///  获取客户列表
        /// </summary>
        /// <param name="context"></param>
        /// <param name="requestType"></param>
        /// <param name="url"></param>
        /// <returns></returns>
        public ResultBase onGetClientList(HttpContext context, RequestType requestType, string url)
        {
            //返回列表集合
            ResultBase result = null;
            try
            {
                //将查询条件封装
                client queryEntity = GetFormEntity<client>();
                result = GetClientList(queryEntity, context);
            }
            catch (Exception ex)
            {
                result = new ResultBase();
                result.Success = false;
                result.Message = ex.Message;
            }
            return result;
        }

        /// <summary>
        /// 获取查询条件的方法
        /// </summary>
        /// <param name="queryEntity">查询条件</param>
        /// <param name="context">请求</param>
        /// <returns></returns>
        public FoothillPageData<client> GetClientList(client queryEntity, HttpContext context)
        {
            string start = context.Request["start"];
            string limit = context.Request["limit"];
            queryEntity.OrderField = context.Request["sort"];
            queryEntity.OrderDirection = context.Request["dir"];
            int intStart = NumericUtil.toInteger(start, 0);
            int intLimit = NumericUtil.toInteger(limit, 1);
            if (intLimit == 0) intLimit = 1;
            int pageNo = intStart / intLimit + 1;
            int pageSize = intLimit;

            return ClientCtrl.GetClientList(pageSize, pageNo, queryEntity);
        }
        #endregion

        #region 保存客户信息
        /// <summary>
        /// 保存客户信息
        /// </summary>
        /// <param name="context"></param>
        /// <param name="requestType"></param>
        /// <param name="url"></param>
        /// <returns></returns>
        public ResultBase onSaveClient(HttpContext context, RequestType requestType, string url)
        {

            //返回列表集合
            ResultBase result = new ResultBase();
            try
            {
                int client_id = 0;
                string clientFileds = context.Request["clientFileds"].Replace('\'', '’').Replace('ψ', '\'');

                if (!string.IsNullOrEmpty(clientFileds))
                    client_id = ClientCtrl.SaveClient(clientFileds);

                result.Success = true;

            }
            catch (Exception ex)
            {
                result = new ResultBase();
                result.Success = false;
                result.Message = ex.Message;
                result.AddErrorDetail((int)GGFW.Utils.Constants.ErrorCode.ERROR_EXCPETION, ex.Message);
                log.Fatal(ex.Message);
            }
            return result;
        }
        #endregion

        #region 更新客户信息
        /// <summary>
        /// 更新产品附加信息
        /// </summary>
        /// <param name="context"></param>
        /// <param name="requestType"></param>
        /// <param name="url"></param>
        /// <returns></returns>
        public ResultBase onUpdateClientInfo(HttpContext context, RequestType requestType, string url)
        {
            //返回列表集合
            ResultBase result = null;
            try
            {
                string client_id = context.Request["ClientID"];
                string filedname = context.Request["FiledName"];
                string filedvalue = context.Request["FiledValue"];
                result = ClientCtrl.UpdateClientInfo(client_id, filedname, filedvalue);

                result.Success = true;
            }
            catch (Exception ex)
            {
                result = new ResultBase();
                result.Success = false;
                result.Message = ex.Message;
                result.AddErrorDetail((int)GGFW.Utils.Constants.ErrorCode.ERROR_EXCPETION, ex.Message);
                log.Fatal(ex.Message);
            }
            return result;
        }
        #endregion

        #region  删除客户
        /// <summary>
        /// 删除客户
        /// </summary>
        /// <param name="context">HttpContext</param>
        /// <param name="requestType">RequestType</param>
        /// <param name="url"></param>
        /// <returns>ResultBase</returns>
        public ResultBase onDelClientByID(HttpContext context, RequestType requestType, string url)
        {
            //返回结果
            ResultBase result = null;
            try
            {
                //从提交的表单获得数据实体
                string client_ids = context.Request["client_ids"].ToString();
                foreach (string client_id in client_ids.Split(','))
                {
                    result = ClientCtrl.DelClientByID(NumericUtil.toInteger(client_id, 0));
                }
                result.success = true;

            }
            catch (Exception ex)
            {
                result = new ResultBase();
                result.Success = false;
                result.Message = ex.Message;
                result.AddErrorDetail((int)GGFW.Utils.Constants.ErrorCode.ERROR_EXCPETION, ex.Message);
            }
            return result;
        }
        #endregion
    }
}
